package com.im.lzj.activity;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.im.lzj.R;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GongActivity extends AppCompatActivity implements GongAdapter.setItemOnClickChange{



    @BindView(R.id.mTv_answer)
    TextView mTv_answer;
    @BindView(R.id.TagFlowLayout)
    com.zhy.view.flowlayout.TagFlowLayout TagFlowLayout;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mBut_Next)
    Button mBut_Next;

    private List<AnswerBean.ResultBean> stringList;

    private GongAdapter adapter;

    private int randNum = 0;

    private String TAG=getClass().getName();

    private TagAdapter tagAdapter;

    private List<String> taglist;

    private List<GongBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gong);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        list = new ArrayList<>();
        taglist = new ArrayList<>();
        stringList = new ArrayList<>();
        adapter = new GongAdapter(GongActivity.this, stringList);

        MyGridLayoutManager gridLayoutManager = new MyGridLayoutManager(GongActivity.this, 3, GridLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(adapter);
        int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.dp_10);
        mRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
        adapter.setSetItemOnClickChange(this);

        tagAdapter=new TagAdapter<String>(taglist) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView mTextView_Tag = (TextView) LayoutInflater.from(GongActivity.this).inflate(R.layout.item_tag_layout, TagFlowLayout, false);
                mTextView_Tag.setText(taglist.get(position));
                return mTextView_Tag;
            }
        };

        TagFlowLayout.setAdapter(tagAdapter);

        TagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                adapter.RefreshItem(taglist.get(position));
                taglist.remove(position);
                tagAdapter.notifyDataChanged();
                return true;
            }
        });

        getData();
    }

    @OnClick({R.id.mBut_Next})
    public void OnClickChange(View view) {
        switch (view.getId()) {
            case R.id.mBut_Next:
                UIData();
                break;
            default:
                break;
        }
    }


    private void UIData() {
        if (stringList != null) {
            stringList.clear();
        }

        if (taglist!=null){
            taglist.clear();
            tagAdapter.notifyDataChanged();
        }

        Random random = new Random();
        randNum = random.nextInt(list.size());
        AnswerBean.ResultBean answerBean;
        String[] a=shuffleForSortingString(list.get(randNum).getAnswer() + list.get(randNum).getInterfere()).split("");
        for (int i = 1; i < a.length; i++) {
            answerBean = new AnswerBean.ResultBean();
            answerBean.setStr(a[i]);
            answerBean.setType("1");
            stringList.add(answerBean);
        }
        mTv_answer.setText(list.get(randNum).getQuestion());

        adapter.notifyDataSetChanged();
    }

    /**
     * 初始化数据
     */
    private void getData() {
        GongBean gongBean = new GongBean();
        gongBean.setId(0);
        gongBean.setAnswer("12345");
        gongBean.setQuestion("中间填______数字？");
        gongBean.setInterfere("6789");
        list.add(gongBean);
        GongBean gongBean1 = new GongBean();
        gongBean1.setId(1);
        gongBean1.setAnswer("老李是不帅");
        gongBean1.setQuestion("怎么夸老李？");
        gongBean1.setInterfere("刘小丑鸭");
        list.add(gongBean1);
        GongBean gongBean2 = new GongBean();
        gongBean2.setId(2);
        gongBean2.setAnswer("12345");
        gongBean2.setQuestion("这是第三题？");
        gongBean2.setInterfere("6789");
        list.add(gongBean2);

        UIData();
    }

    public String shuffleForSortingString(String s) {
        char[] c = s.toCharArray();
        List<Character> lst = new ArrayList<Character>();
        for (int i = 0; i < c.length; i++) {
            lst.add(c[i]);
        }

        System.out.println(lst);

        Collections.shuffle(lst);

        System.out.println(lst);

        String resultStr = "";
        for (int i = 0; i < lst.size(); i++) {
            resultStr += lst.get(i);
        }

        System.out.println(resultStr);
        return resultStr;
    }

    @Override
    public void DoClick(View view, int position) {
        switch (view.getId()) {
            case R.id.mRelative:

                for (int i = 0; i < taglist.size(); i++) {
                    if (taglist.get(i).equals(stringList.get(position).getStr())){
                        //该字已选择，视为取消选择
                        adapter.RefreshItem(stringList.get(position).getStr());
                        taglist.remove(i);
                        tagAdapter.notifyDataChanged();
                        return;
                    }
                }

                if (taglist.size()==5){
                    //已经选择了5个字

                }else {
                    taglist.add(stringList.get(position).getStr());
                    tagAdapter.notifyDataChanged();
                    adapter.RefreshItem(position);

                    if (taglist.size()==5){
                        //判断是否是正确答案
                        IsAnswer();
                    }
                }
                break;
            default:
                break;
        }
    }

    /**
     * 判断答案是否正确
     */
    private void IsAnswer() {
        StringBuffer stringBuffer=new StringBuffer();
        for (int i = 0; i < taglist.size(); i++) {
            stringBuffer.append(taglist.get(i));
        }

        if (stringBuffer.toString().equals(list.get(randNum).getAnswer())){
            //回答正确
            Toast.makeText(GongActivity.this,"回答正确",Toast.LENGTH_SHORT).show();
        }else {
            //回答错误
            Toast.makeText(GongActivity.this,"回答错误",Toast.LENGTH_SHORT).show();
        }
    }
}
